package com.example.image.glide

import android.content.Context
import android.graphics.Bitmap
import android.view.View
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.example.image.R
import jp.wasabeef.glide.transformations.BlurTransformation
import jp.wasabeef.glide.transformations.RoundedCornersTransformation

/**
 * 图片加载工具
 */
object GlideImgLoader {
    //普通图片
    fun loadImage(context: Context, url:String, imageView: ImageView){
        Glide.with(context)
            .load(url)
            .error(R.drawable.ic_yu_icon)
            .placeholder(R.drawable.ic_yu_icon)
            .skipMemoryCache(false)
            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
            .into(imageView)
    }
    //圆形图片
    fun loadCircleImage(context: Context, url:String, imageView: ImageView){
        Glide.with(context)
            .load(url)
            .error(R.drawable.ic_yu_icon)
            .placeholder(R.drawable.ic_yu_icon)
            .skipMemoryCache(false)
            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
            .circleCrop()
            .into(imageView)
    }
    //圆角图片
    fun loadCornerImage(context: Context, url:String, imageView: ImageView, radius:Int){
        Glide.with(context)
            .load(url)
            .error(R.drawable.ic_yu_icon)
            .placeholder(R.drawable.ic_yu_icon)
            .skipMemoryCache(false)
            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
            .transform(RoundedCorners(radius))
            .into(imageView)
    }
    //加载本地资源图片
    fun loadResImage(view: View, resId: Int, imageView:ImageView){
        Glide.with(view)
            .load(resId)
            .skipMemoryCache(true)
            .transform(CenterCrop())
            .into(imageView)
    }
    //毛玻璃效果
    fun loadBlurImage(context: Context, url:String, imageView: ImageView){
        Glide.with(context)
            .load(url)
            .error(R.drawable.ic_yu_icon)
            .placeholder(R.drawable.ic_yu_icon)
            .skipMemoryCache(false)
            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
            .apply (RequestOptions.bitmapTransform(MultiTransformation<Bitmap>(
                BlurTransformation(10,3),
                RoundedCornersTransformation(128,0,RoundedCornersTransformation.CornerType.BOTTOM)
            )))
            .into(imageView)
    }
}